package com.wws.family.ui.fragment

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.bumptech.glide.Glide
import com.wika.basics.base.BaseFragment
import com.wika.basics.ext.vertical
import com.wws.family.R
import com.wws.family.adapter.RecentAdapter
import com.wws.family.databinding.FragmentHomeBinding
import com.wws.family.db.WiKaDataBase
import com.wws.family.ui.activity.AccountActivity
import com.wws.family.ui.activity.MedicineActivity
import com.wws.family.ui.activity.NoteBookActivity
import com.wws.family.utils.DateUtils
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 *@Description: 主页Fragment
 *@Date: 2025/1/18 11:20
 *@Author: WangWeiShuo
 */
class HomeFragment: BaseFragment<FragmentHomeBinding>() {
    private var recentAdapter: RecentAdapter? = null

    override fun initView(view: View, savedInstanceState: Bundle?) {
        initImageBanner()
        mBinding.btnMedicine.setOnClickListener {
            startActivity(Intent(requireContext(), MedicineActivity::class.java))
        }
        mBinding.btnAccount.setOnClickListener {
            startActivity(Intent(requireContext(), AccountActivity::class.java))
        }
        mBinding.btnCommemoration.setOnClickListener {
            startActivity(Intent(requireContext(), NoteBookActivity::class.java))
        }
    }

    override fun initData() {
        initRecent()
    }

    override fun onNewIntent() {

    }

    private fun initImageBanner(){
        val imageBanner = mBinding.imageBanner
        val images = listOf(
            R.drawable.bg_s1, R.drawable.bg_s2, R.drawable.bg_s3
        )
        imageBanner.adapter = object : BannerImageAdapter<Int>(images) {
            override fun onBindView(
                holder: BannerImageHolder?,
                data: Int?,
                position: Int,
                size: Int
            ) {
                holder?.let {
                    Glide.with(requireContext())
                        .load(data)
                        .into(holder.imageView)
                }
            }
        }

        // 开启自动循环轮播
        imageBanner.isAutoLoop(true)
        //设置指示器
        imageBanner.indicator = CircleIndicator(requireContext())
        //设置滚动条淡入淡出持续时间
        imageBanner.scrollBarFadeDuration = 1000
        // 设置指示器颜色
        imageBanner.setIndicatorSelectedColor(Color.GREEN)
        imageBanner.start()
    }

    private fun initRecent(){
        if(recentAdapter == null) {
            recentAdapter = RecentAdapter(requireContext())
            mBinding.rvRecent.vertical()
            mBinding.rvRecent.adapter = recentAdapter
        }
        lifecycleScope.launch(Dispatchers.IO){
            val recentList = WiKaDataBase.getInstance().medicineDao().getMedicineInfoByTime(
                DateUtils.getNowDateNoHeng())
            withContext(Dispatchers.Main){
                if(recentAdapter != null){
                    recentAdapter?.setData(recentList)
                }
            }
        }

//        recentAdapter.setData()
    }
}